package com.tuanzi.service.impl;

import com.tuanzi.dao.TemperatureDayMapper;
import com.tuanzi.dao.TemperatureWeekMapper;
import com.tuanzi.service.MqttService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tuanzi
 * @version 1.0
 * @date 2022/4/25 20:34
 */

@Service
public class MqttServiceImpl implements MqttService {

    @Resource
    TemperatureDayMapper temperatureDayMapper;

    @Resource
    TemperatureWeekMapper temperatureWeekMapper;

    /**
     * 把当前的温度插入数据库
     */
    @Override
    public Integer insertTemperature(Float temperature,Integer fid) {

        Integer result = temperatureDayMapper.insertTemperatureById(temperature,fid);

        // 判断数据库中记录数量 达到八自动删除第一个
        Integer count = temperatureDayMapper.countTemperature();
        if (count > 8){
            // 删除最前面一条温度记录
            Integer end = temperatureDayMapper.deleteFirstTemperature();
            if (end != 1){
                return 0;
            }
        }

        // 计算平均值，并且赋值给temperatureWeek的今天的平均温度
        // 获取今天所有的温度信息

        return result;
    }

}
